<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /**
 * @param {number[]} nums
 * @return {number[][]}
 */
        var permute = function (nums) {
            let numsLength = nums.length;
            //本元素以及除本元素之外的元素的全排列
            let res = [];
            let arr = [];
            let visited = [];
            for (let i = 0; i < numsLength; i++) {
                visited[i] = 0;
            }
            const func = function () {
                if (arr.length === numsLength) {
                    res.push([...arr]);
                    // console.log('res', res)
                    return;
                }
                for (let i = 0; i < numsLength; i++) {
                    if (visited[i] !== 1) {
                        arr.push(nums[i]);
                        visited[i] = 1;
                        func();
                        visited[i] = 0
                        arr.pop(nums[i]);
                    }
                }
            }
            func();
            return res;
        };
        nums=[1,2,3];
        permute(nums);
    </script>
</body>

</html>